/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MenuPrincipal.java
 *
 * Created on 09/02/2010, 15:40:10
 */
package br.com.Visao;

import br.com.ClassesPersistentes.Fornecedor;
import br.com.ClassesPersistentes.Produto;
import br.com.ClassesPersistentes.Unidade;
import br.com.Controle.ProdutoControle;
import br.com.ModeloTabela.ProdutoModeloTabela;
import br.com.Persistencia.PersistenciaConexao;
import java.util.List;
import javax.persistence.EntityManager;
import javax.swing.JOptionPane;

/**
 *
 * @author Daniel
 */
public class MenuPrincipal extends javax.swing.JFrame {

    /** Creates new form MenuPrincipal */
    public MenuPrincipal() {
        initComponents();
        //setExtendedState(this.MAXIMIZED_BOTH);
    }
    Produto prod;

    public void limpar() {
        jtCodigo.setText("");
        jfCodigoFornecedor.setText("");
        jfDescricaoProduto.setText("");
        jfUnidade.setText("");
        jfValor.setText("");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        Tabela = new javax.swing.JTable();
        Criar = new javax.swing.JButton();
        Recuperar = new javax.swing.JButton();
        Alterar = new javax.swing.JButton();
        Deletar = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jtLocalizarCodigo = new javax.swing.JTextField();
        jtCodigo = new javax.swing.JFormattedTextField();
        jfDescricaoProduto = new javax.swing.JFormattedTextField();
        jfUnidade = new javax.swing.JFormattedTextField();
        jfCodigoFornecedor = new javax.swing.JFormattedTextField();
        jfValor = new javax.swing.JFormattedTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("DEFININDO ESTRUTURA DO PROJETO SGPS");
        setBackground(new java.awt.Color(204, 204, 255));

        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        Tabela.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(Tabela);

        jPanel1.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 260, 524, 100));

        Criar.setText("C");
        Criar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CriarActionPerformed(evt);
            }
        });
        jPanel1.add(Criar, new org.netbeans.lib.awtextra.AbsoluteConstraints(440, 20, -1, -1));

        Recuperar.setText("R");
        Recuperar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RecuperarActionPerformed(evt);
            }
        });
        jPanel1.add(Recuperar, new org.netbeans.lib.awtextra.AbsoluteConstraints(440, 50, -1, -1));

        Alterar.setText("U");
        Alterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AlterarActionPerformed(evt);
            }
        });
        jPanel1.add(Alterar, new org.netbeans.lib.awtextra.AbsoluteConstraints(440, 80, -1, -1));

        Deletar.setText("D");
        Deletar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DeletarActionPerformed(evt);
            }
        });
        jPanel1.add(Deletar, new org.netbeans.lib.awtextra.AbsoluteConstraints(440, 110, -1, -1));

        jtCodigo.setBackground(new java.awt.Color(255, 255, 204));

        jfDescricaoProduto.setBackground(new java.awt.Color(255, 255, 204));

        jfUnidade.setBackground(new java.awt.Color(255, 255, 204));

        jfCodigoFornecedor.setBackground(new java.awt.Color(255, 255, 204));

        jfValor.setBackground(new java.awt.Color(255, 255, 204));

        jLabel1.setText("Código para Pesquisa: ");

        jLabel2.setText("Código");

        jLabel3.setText("Descrição do Produto");

        jLabel4.setText("Cód. Unid. Med.");

        jLabel5.setText("Código Fornecedor");

        jLabel6.setText("R$ Valor de Venda");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(8, 8, 8)
                        .addComponent(jtLocalizarCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(51, 51, 51)
                        .addComponent(jLabel3))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jfDescricaoProduto, javax.swing.GroupLayout.PREFERRED_SIZE, 230, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(9, 9, 9)
                        .addComponent(jLabel5))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jfUnidade, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(28, 28, 28)
                        .addComponent(jfCodigoFornecedor, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel6)
                    .addComponent(jfValor, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(28, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jtLocalizarCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addGap(4, 4, 4)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jfDescricaoProduto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(8, 8, 8)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5))
                .addGap(4, 4, 4)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jfUnidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jfCodigoFornecedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(7, 7, 7)
                .addComponent(jLabel6)
                .addGap(5, 5, 5)
                .addComponent(jfValor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(15, Short.MAX_VALUE))
        );

        jPanel1.add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 20, 360, 210));

        jButton1.setText("Limpar Campos");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(400, 150, -1, -1));

        jButton2.setText("Listar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 230, -1, -1));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 585, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-603)/2, (screenSize.height-425)/2, 603, 425);
    }// </editor-fold>//GEN-END:initComponents

    private void CriarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CriarActionPerformed


        if (prod == null) {
            //JOptionPane.showMessageDialog(null, "nulo...");
            prod = new Produto();
        } else {
            prod = null;
            prod = new Produto();
            //JOptionPane.showMessageDialog(null, "em uso...");
        }

        Unidade un = new Unidade();
        Fornecedor forne = new Fornecedor();
        EntityManager em = PersistenciaConexao.entityManager();
        un = em.find(Unidade.class, Integer.parseInt(jfUnidade.getText()));
        forne = em.find(Fornecedor.class, Integer.parseInt(jfCodigoFornecedor.getText()));

        prod.setCodProduto(jtCodigo.getText());
        prod.setDescricaoProduto(jfDescricaoProduto.getText());
        prod.setCodUnidade(un);
        prod.setCodFornecedor(forne);
        prod.setVlrVendaProduto(Double.parseDouble(jfValor.getText()));

        ProdutoControle pp = new ProdutoControle();
        if (pp.salvar(prod)) {
            JOptionPane.showMessageDialog(null, "Excutado com Sucesso");
            limpar();
        } else {
            JOptionPane.showMessageDialog(null, "Ocorreu Erro");
        }
    }//GEN-LAST:event_CriarActionPerformed

    private void RecuperarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RecuperarActionPerformed

        if (prod == null) {
            prod = new Produto();
        }

        ProdutoControle pp = new ProdutoControle();

        prod = pp.localizar(jtLocalizarCodigo.getText());
        limpar();
        if (prod != null) {
            jtCodigo.setText(String.valueOf(prod.getCodProduto()));
            jfDescricaoProduto.setText(prod.getDescricaoProduto());
            jfCodigoFornecedor.setText(String.valueOf(prod.getCodFornecedor().getCodFornecedor()));
            jfUnidade.setText(String.valueOf(prod.getCodUnidade().getCodUnidade()));
            jfValor.setText(Double.toString(prod.getVlrVendaProduto()));
        }



        //JOptionPane.showMessageDialog(null, "Descrição da Unidade: "+prod.getDescricaoProduto());
    }//GEN-LAST:event_RecuperarActionPerformed

    private void AlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AlterarActionPerformed


        if (prod == null) {
            //JOptionPane.showMessageDialog(null, "nulo...");
            prod = new Produto();
        } else {
            prod = null;
            prod = new Produto();
            //JOptionPane.showMessageDialog(null, "em uso...");
        }

        Unidade un = new Unidade();
        Fornecedor forne = new Fornecedor();
        EntityManager em = PersistenciaConexao.entityManager();
        un = em.find(Unidade.class, Integer.parseInt(jfUnidade.getText()));
        forne = em.find(Fornecedor.class, Integer.parseInt(jfCodigoFornecedor.getText()));

        prod.setCodProduto(jtCodigo.getText());
        prod.setDescricaoProduto(jfDescricaoProduto.getText());
        prod.setCodUnidade(un);
        prod.setCodFornecedor(forne);
        prod.setVlrVendaProduto(Double.parseDouble(jfValor.getText()));

        ProdutoControle pp = new ProdutoControle();
        
        if (pp.alterar(prod)) {
            JOptionPane.showMessageDialog(null, "Excutado com Sucesso");
            limpar();
        } else {
            JOptionPane.showMessageDialog(null, "Ocorreu Erro");
        }
}//GEN-LAST:event_AlterarActionPerformed

    private void DeletarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DeletarActionPerformed

        ProdutoControle pp = new ProdutoControle();
        //if (pp.delete(Integer.parseInt(jtLocalizarCodigo.getText())))


        if (pp.excluir(jtLocalizarCodigo.getText())) {
            JOptionPane.showMessageDialog(null, "Excutado com Sucesso");
            limpar();
        } else {
            JOptionPane.showMessageDialog(null, "Ocorreu Erro");
            limpar();
        }
    }//GEN-LAST:event_DeletarActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        limpar();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed

        ProdutoControle pp = new ProdutoControle();
        List<Produto> lprod = pp.findAll();
        ProdutoModeloTabela pdt = new ProdutoModeloTabela(lprod);
        Tabela.setModel(pdt);
    }//GEN-LAST:event_jButton2ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MenuPrincipal().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Alterar;
    private javax.swing.JButton Criar;
    private javax.swing.JButton Deletar;
    private javax.swing.JButton Recuperar;
    private javax.swing.JTable Tabela;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JFormattedTextField jfCodigoFornecedor;
    private javax.swing.JFormattedTextField jfDescricaoProduto;
    private javax.swing.JFormattedTextField jfUnidade;
    private javax.swing.JFormattedTextField jfValor;
    private javax.swing.JFormattedTextField jtCodigo;
    private javax.swing.JTextField jtLocalizarCodigo;
    // End of variables declaration//GEN-END:variables
}
